package uid

import (
	"fmt"
	"crypto/sha1"
	"io"
	"io/ioutil"
)

type AgentId struct {
	Idc                 int                 // 域
	Sequence            int64
	NanoTimeStamp       int64               // Nano纳秒级别的时间戳
}

/**
 用于生成40位的 worm id 值
 */
func (a AgentId) Sha1() string {
	origin := fmt.Sprintf("%3d%20d%40d", a.Idc, a.Sequence, a.NanoTimeStamp)

	t := sha1.New()
	io.WriteString(t, origin)
	return fmt.Sprintf("%x", t.Sum(nil))
}

func GetAgentIdFromFile(dirPath string) string {

	dir, err := ioutil.ReadDir(dirPath)
	if err != nil {
		return ""
	} else {

	}

	for _, item := range dir {

		if item.IsDir() {
			continue
		} else {
			if len(item.Name()) == 40 {             // 40是id的长度，算法不变，长度不变
				return item.Name()
			}
		}
	}

	return ""
}